//
//  CalculatorEngine.swift
//  Calculator
//
//  Created by 白瑾浩 on 2022/1/12.
//

import UIKit

class CalculatorEngine: NSObject {
    let funcArray:CharacterSet = ["+","-","*","/","%","^"]
    
    func calculatEquation(equation:String)->Double{
        //以运算分割字符串来获取数字数组
        let elementArray = equation.components(separatedBy: funcArray )
        
        var tip = 0
        
        var result:Double = Double(elementArray[0])!
        
        for char in equation{
            
            switch char {
            case "+":
                tip += 1
                if elementArray.count>tip {
                        result += Double(elementArray[tip])!
                }
            case "-":
                tip += 1
                if elementArray.count>tip {
                        result -= Double(elementArray[tip])!
                }
            case "*":
                tip += 1
                if elementArray.count>tip {
                        result *= Double(elementArray[tip])!
                }
            case "/":
                tip += 1
                if elementArray.count>tip {
                        result /= Double(elementArray[tip])!
                }
            case "%":
                tip += 1
                if elementArray.count>tip {
                        result = Double( Int(result) % Int(elementArray[tip])! )
                }
            case "^": //指数运算符
                tip += 1
                if elementArray.count>tip {
                    let tmp = result
                    for _ in 1..<Int(elementArray[tip])!{
                        result *= tmp
                    }
                }
            default:
                break
                    
            }
            
        }
        return result
    
    }
    
}
